package com.saifan.wyy_ov.ui.communitycenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.PayRecordBean;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-05-05 14:41
 * @descrption 我的缴费记录
 */
public class PaythefeesActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private RefreshLayout refreshLayout;
    private ListView listView;
    private List<PayRecordBean> beans = new ArrayList<>();
    private CommonAdapter<PayRecordBean> adapter;
    private GetData getData;
    private int page;
    private RoomInfoBean roomInfoBean;
    private View loadlay;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomInfoBean = getRoomInfoBean();
        getData = new GetData();
        setContentView(R.layout.activity_paythefees);
        loadlay = findViewById(R.id.loading_lay);
        this.refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initAdapter();
        query();


    }

    private void initAdapter() {
        adapter = new CommonAdapter<PayRecordBean>(this, beans, R.layout.paythefees_item) {
            @Override
            public void convert(ViewHolderHelper helper, PayRecordBean item) {
                if (!StringUtil.isNullOrEmpty(item.getWYPZ_SKSJ())) {
                    item.setWYPZ_SKSJ(item.getWYPZ_SKSJ().replace("T", " "));
                }
                helper.setText(R.id.SJ, StringUtil.returnShow(item.getWYPZ_SKSJ()));
                helper.setText(R.id.SKBH, "凭证号：" + StringUtil.returnShow(item.getWYPZ_SKBH()));
                helper.setText(R.id.JE, getString(R.string.rmb ,PriceUtils.getPrice(item.getWYPZ_SKJE()) ) );
                helper.setText(R.id.type, StringUtil.returnShow(item.getWYPZ_SKLX()));
            }
        };
        listView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {

                refreshLayout.setLoading(true);
                page++;
                query();
            }
        });
        listView.setOnItemClickListener(this);

    }

    public void query() {

        try {
            if (beans .isEmpty()) {
                loadlay.setVisibility(View.VISIBLE);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("FKGX_ZJ", roomInfoBean.getFKGX_ZJ());
            jsonObject.put("Page", page);

            getData.getDataJson(this, Api.Cost.MY_PAY_COST_INFO, jsonObject, "", new ResponseData() {
                @Override
                public void onSuccess(String json) {

                    List<PayRecordBean> list = new Gson().fromJson(json, new TypeToken<List<PayRecordBean>>() {
                    }.getType());

                    if (list != null && !list.isEmpty()) {
                        if (page == 0) {
                            beans = list;
                        } else {
                            beans.addAll(list);
                        }
                        adapter.mDatas = beans;
                        adapter.notifyDataSetChanged();

                    } else {
                        if (page > 0) {
                            page--;
                        } else {

                        }

                    }
                    loadlay.setVisibility(View.GONE);
                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);

                }

                @Override
                public void onFailed(String str) {

                    loadlay.setVisibility(View.GONE);
                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                    if (page > 0) page--;

                }
            });
        } catch (JSONException e) {
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.putExtra("data", beans.get(position));
        intent.setClass(this, PaythefeesInfoActivity.class);
        startActivity(intent);

    }
}
